/*
 * 文件名：SupplierServiceImpl.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：wuliquan
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.consumables.supplier.service.impl;

import javax.servlet.http.HttpServletRequest;

import org.springframework.dao.DataAccessException;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.Result;
import com.wootide.cms.common.util.CommonUtil;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.consumables.supplier.dao.SupplierInfoDao;
import com.wootide.cms.service.consumables.supplier.pojo.SupplierInfo;
import com.wootide.cms.service.consumables.supplier.service.SupplierInfoService;
import com.wootide.cms.service.consumables.supplier.transaction.SupplierInfoTransaction;

/**
 * 供应商信息管理业务层接口实现
 * 〈功能详细描述〉
 * @author wuliquan
 * @version 2013-2-23
 * @see SupplierInfoServiceImpl
 * @since
 */
public class SupplierInfoServiceImpl implements SupplierInfoService
{
    /**
     * 运行日志实例
     */
    private RunLog runLog;
    
    /**
     * 供应商事务层实例
     */
    private SupplierInfoTransaction supplierInfoTransaction;
    
    /**
     * 供应商数据访问层实例
     */
    private SupplierInfoDao supplierInfoDao;
    
    public Result addOneSupplier(SupplierInfo supplierInfo,
            HttpServletRequest request)
    {
        
        // 打印方法入口参数
        runLog.debug("supplierInfo=" + supplierInfo);
        Result result = new Result();
        try
        {
            supplierInfoTransaction.addOneSupplier(supplierInfo);
            runLog.info("Add SupplierInfo Success! Input data:" + supplierInfo,
                    request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        catch (DataAccessException e)
        {
            e.printStackTrace();
            //数据库异常
            runLog.error("Add SupplierInfo Failed! Input data:" + supplierInfo,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //运行时异常
            runLog.info("Add SupplierInfo Failed! Input Data:" + supplierInfo
                    + " Error Message:" + e.getMessage(), request);
            
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            e.printStackTrace();
            //未知异常
            runLog.error("Add SupplierInfo Failed!Input data:" + supplierInfo,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
        
    }
    
    public Result checkContentWhenAdd(String content)
    {
        // 打印方法入口参数
        runLog.debug("content=" + content);
        
        int num = supplierInfoDao.findNumBySupplierName(content);
        Result result = new Result();
        // 如果已存在
        if (num > 0)
        {
            runLog.info("Illegal!SupplierInfo Has Existed! Input Data:"
                    + content);
            
            result.setResultCode(ErrorCodes.Consumables.Supplier.SUPPLIER_EXIST);
        }
        // 如果不存在
        else
        {
            runLog.info("Legal!SupplierInfo Not Existed! Input Data:" + content);
            
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        }
        return result;
        
    }
    
    public Result delSuppliers(Integer[] innerIndexs, HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("innerIndexs = " + CommonUtil.toString(innerIndexs));
        
        Result result = new Result();
        
        result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
        // 定义删除失败的条数
        int delErrorNum = 0;
        
        for (Integer innerIndex : innerIndexs)
        {
            SupplierInfo supplier = null;
            try
            {
                // 调用删除路由事务层
                supplier = supplierInfoTransaction.delOneSupplier(innerIndex);
                runLog.info("Delete SupplierInfo Success! Supplier = "
                        + supplier, request);
                
            }
            catch (DataAccessException e)
            {
                //数据库异常
                delErrorNum++;
                runLog.error("Delete SupplierInfo Failed! Innerindex = "
                        + innerIndex, e, request);
                
                result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
                
            }
            catch (CMSRuntimeException e)
            {
                //运行时异常
                runLog.info("Delete SupplierInfo Failed! Innerindex = "
                        + innerIndex + " Error Message:" + e.getMessage(),
                        request);
                result.setResultCode(e.getErrorCode());
                
            }
            catch (Exception e)
            {
                //未知异常
                delErrorNum++;
                runLog.error("Delete SupplierInfo Failed!  Innerindex = "
                        + innerIndex, e, request);
                result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
                
            }
        }
        // 返回失败的条数
        result.setDelErrorNum(delErrorNum);
        return result;
    }
    
    public Result modOneSupplier(SupplierInfo supplierInfo,
            HttpServletRequest request)
    {
        // 打印方法入口参数
        runLog.debug("supplierInfo=" + supplierInfo);
        Result result = new Result();
        try
        {
            SupplierInfo oldSupplier = supplierInfoTransaction.modOneSupplier(supplierInfo);
            runLog.info("Mod SupplierInfo Success! Input data:" + supplierInfo
                    + "|oldDate:" + oldSupplier, request);
            
            // 设置返回码为成功
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Mod SupplierInfo Failed! Input data:" + supplierInfo,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod SupplierInfo Failed! Input Data:" + supplierInfo
                    + " Error Message:" + e.getMessage(), request);
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            //位置异常
            runLog.error("Mod SupplierInfo Failed!Input data:" + supplierInfo,
                    e,
                    request);
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result queryForPages(Page page, QueryCondition queryCondition)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryCondition=" + queryCondition);
        
        // 定义返回结果
        Result result = new Result();
        try
        {
            queryCondition = null == queryCondition ? new QueryCondition()
                    : queryCondition;
            queryCondition.setObjClass(SupplierInfo.class);
            QueryResults queryResults = supplierInfoTransaction.queryForPages(page,
                    queryCondition);
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query SupplierInfo Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query SupplierInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query SupplierInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public Result queryAllSuppliers()
    {
        // 定义返回结果
        Result result = new Result();
        try
        {
            QueryResults queryResults = supplierInfoDao.findAllSuppliers();
            result.setResultPojo(queryResults);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            runLog.info("Query SupplierInfo Success!");
        }
        catch (DataAccessException e)
        {
            //数据库异常
            runLog.error("Query SupplierInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        
        catch (Exception e)
        {
            //异常
            runLog.error("Query SupplierInfo Failed!", e);
            // 注册修改前的路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }

    public Result viewOneSupplier(Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        
        Result result = new Result();
        try
        {
            SupplierInfo supplier = supplierInfoTransaction.viewOneSupplier(innerIndex);
            result.setResultCode(ErrorCodes.ERRORCODE_SUCC);
            result.setResultPojo(supplier);
        }
        catch (DataAccessException e)
        {
            runLog.error("Find One SupplierInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_DATABASE);
        }
        catch (CMSRuntimeException e)
        {
            //自定义异常
            runLog.info("Mod SupplierInfo Failed! Input Data:" + innerIndex
                    + " Error Message:" + e.getMessage());
            result.setResultCode(e.getErrorCode());
        }
        catch (Exception e)
        {
            runLog.error("Find One SupplierInfo Failed!", e);
            // 注册修改前的中文路由对象
            result.setResultCode(ErrorCodes.ERRORCODE_UNKNOW);
        }
        return result;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
    public void setSupplierInfoTransaction(
            SupplierInfoTransaction supplierInfoTransaction)
    {
        this.supplierInfoTransaction = supplierInfoTransaction;
    }
    
    public void setSupplierInfoDao(SupplierInfoDao supplierInfoDao)
    {
        this.supplierInfoDao = supplierInfoDao;
    }
}
